package j_util.graphics.gui;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
public class GraphicDialog extends JDialog {
    private DrawPlotPanel drawPlotPanel;
    private javax.swing.JButton saveButton;
    private javax.swing.JSeparator separator;
    private javax.swing.JSeparator separatorBottom;
    private javax.swing.JLabel titleLabel;

    public GraphicDialog(double[] x, double[] y, double scale) {
        initComponents(x, y, scale);
    }

    private void initComponents(double[] x, double[] y, double scale) {

        drawPlotPanel = new DrawPlotPanel(x, y, scale, "");
        titleLabel = new javax.swing.JLabel();
        separator = new javax.swing.JSeparator();
        saveButton = new javax.swing.JButton();
        separatorBottom = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        titleLabel.setText("Graphic");

        saveButton.setText("Save graphic");
        saveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int returnValue = fileChooser.showSaveDialog(new JFrame()); //todo change for parent container
                if (returnValue == JFileChooser.APPROVE_OPTION) {
                    try {
                        File file = fileChooser.getSelectedFile().getAbsoluteFile();
                        String fileName = file.getAbsolutePath();
                        System.out.println("" + fileName);
                        System.out.println("" + (!fileName.substring(fileName.length() - 4, fileName.length()).equalsIgnoreCase(".png")));
                        if (!fileName.substring(fileName.length() - 4, fileName.length()).equalsIgnoreCase(".png")) {
                            file = new File(fileName + ".png");
                        }
                        ImageIO.write(drawPlotPanel.getBufferedImage(), "png", file);
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(titleLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(separator, javax.swing.GroupLayout.DEFAULT_SIZE, 556, Short.MAX_VALUE)
                                .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(separatorBottom, javax.swing.GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(saveButton))
                        .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(drawPlotPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 592, Short.MAX_VALUE)
                        .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(titleLabel)
                                .addGroup(layout.createSequentialGroup()
                                .addGap(11, 11, 11)
                                .addComponent(separator, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(drawPlotPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 515, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(saveButton)
                        .addComponent(separatorBottom, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        pack();
    }
}
